package com.project.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.project.dao.implement.AccountTreeSubMenuImpl;

public class AccountTreeSubMenuServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AccountTreeSubMenuServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html/json;charset=utf-8");
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        String bankName =request.getParameter("bankName");
        List<String> bankNames = new ArrayList<String>();
        if (bankName.equals("招商银行"))
		{
        	bankNames.add("%招%");
		}
		else if (bankName.equals("交通银行"))
		{
			bankNames.add("%交%");
		}
		else if (bankName.equals("中国银行"))
		{
			bankNames.add("%中国银行%");
		}
		else if (bankName.equals("工商银行"))
		{
			bankNames.add("%工%");
		}
		else if (bankName.equals("广发银行"))
		{
			bankNames.add("%广发%");
			bankNames.add("%广东发展%");
		}
		else if (bankName.equals("浦发银行"))
		{
			bankNames.add("%浦发%");
			bankNames.add("%浦东发展%");
		}
		else if (bankName.equals("华夏银行"))
		{
			bankNames.add("%华夏银行%");
		}
		else if (bankName.equals("农业银行"))
		{
			bankNames.add("%农业银行%");
			bankNames.add("%农行%");
		}
		else if (bankName.equals("民生银行"))
		{
			bankNames.add("%民生%");
		}
		else if (bankName.equals("光大银行"))
		{
			bankNames.add("%光大%");
		}
		else if (bankName.equals("汉口银行"))
		{
			bankNames.add("%汉口银行%");
		}
		else if (bankName.equals("长沙银行"))
		{
			bankNames.add("%长沙%");
		}
		else if (bankName.equals("深圳银行"))
		{
			bankNames.add("%深发%");
			bankNames.add("%深圳发展%");
		}
		else if (bankName.equals("农村信用合作社"))
		{
			bankNames.add("%农村信用%");
		}
		else if (bankName.equals("兴业银行"))
		{
			bankNames.add("%兴业%");
		}
		else
		{
			bankNames.add("其他银行");
		}
        
        AccountTreeSubMenuImpl menuMapper = new AccountTreeSubMenuImpl();
        
        Iterator bankIt = bankNames.iterator();
        List<String> menuList = new ArrayList<String>();
        List<String> temp = null;
        while(bankIt.hasNext()){
        	String bkName = bankIt.next().toString();
        	if (bkName.equals("其他银行"))
        	{
        		temp = menuMapper.GetUserAccountListByOtherBank();
        		menuList.addAll(temp);
        		continue;
           	}
        	temp = menuMapper.GetUserAccountList(bkName);
    		menuList.addAll(temp);
        }
        
        JSONObject json = new JSONObject(); 
    	try{    
    		JSONArray jsonArray =new JSONArray();
    		
    		Iterator i = menuList.iterator();
    		
    		while(i.hasNext()){  
    			jsonArray.put(i.next());  
            } 
    		json.put("jsonArray", jsonArray);
    	}
    	catch(Exception e){               
    		e.printStackTrace();           
    	}           
    	        
    	response.getWriter().write(json.toString());  
    	response.getWriter().close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
